package com.xhs.main.controller.yfb;

import com.xhs.main.entity.result.LayuiResponseResult;
import com.xhs.main.entity.yfb.YfbBook;
import com.xhs.main.entity.yfb.YfbItem;
import com.xhs.main.entity.yfb.YfbUser;
import com.xhs.main.service.yfb.YfbItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @create 2020-10-22 14:33
 */
@RestController
public class ItemController {
    @Autowired
    YfbItemService yfbItemService;

    //创建项目
    @RequestMapping("/yfb/createItem")
    public LayuiResponseResult createItem(YfbItem yfbItem, YfbBook yfbBook, String yfbGroupName) {  //接收项目，计划书，分组表组名
        yfbItemService.createItem(yfbItem, yfbBook, yfbGroupName);  //将项目，计划书，分组组名传下去
        return new LayuiResponseResult(0, "", 1, null);
    }

    //修改项目
    @RequestMapping("/yfb/updateItem")
    public LayuiResponseResult updateItem(YfbItem yfbItem) {
        yfbItemService.updateItem(yfbItem);  //修改项目
        return new LayuiResponseResult(0, "", 1, null);
    }

    //查看所有项目
    @RequestMapping("/yfb/allItems")
    public LayuiResponseResult queryAllItems() {
        List<YfbItem> yfbItems = yfbItemService.queryAllItems();
        return new LayuiResponseResult(0, "", yfbItems.size(), yfbItems);
    }

    //根据登录用户查
    @RequestMapping("/yfb/queryItemByUser")  //用户负责项目
    public LayuiResponseResult queryItemByUserNumber(HttpSession session) {
        //从session域中获取用户的编号
        YfbUser yfbUser = (YfbUser) session.getAttribute("user");
        //根据用户编号,查责任表，查出负责的项目编号,再查项目表，查负责的项目。
        List<YfbItem> yfbItems = yfbItemService.queryItemByLoginUserNumber(yfbUser.getYfbUserUsernumber());
        //根据项目编号查项目信息
        return new LayuiResponseResult(0, "", yfbItems.size(), yfbItems);
    }

    //根据主键查项目
    @RequestMapping("/yfb/getItemById")
    public LayuiResponseResult getItemById(String yfbItemId) {
        YfbItem yfbItem = yfbItemService.queryItemById(yfbItemId);
        return new LayuiResponseResult(0, "", 1, yfbItem);
    }
}
